#pragma once
#include <QAbstractTableModel>
#include <QStringList>
#include <QDateTime>

// 只给 T_Player 用的字幕数据项
struct PlayerSubtitleItem {
    QDateTime timestamp;
    QString   content;
};

class T_PlayerCaptionModel : public QAbstractTableModel {
    Q_OBJECT
public:
    QVector<PlayerSubtitleItem> m_data;

    explicit T_PlayerCaptionModel(QObject* parent = nullptr)
      : QAbstractTableModel(parent)
    {
        m_headers << "时间" << "内容";
    }

    int rowCount(const QModelIndex& = {}) const override { return m_data.size(); }
    int columnCount(const QModelIndex& = {}) const override { return m_headers.size(); }

    QVariant data(const QModelIndex& idx, int role) const override {
        if (!idx.isValid()) return {};
        const auto &it = m_data[idx.row()];
        if (role == Qt::DisplayRole) {
            if (idx.column() == 0) return it.timestamp.time().toString("hh:mm:ss.zzz");
            else                  return it.content;
        }
        if (role == Qt::TextAlignmentRole) return Qt::AlignCenter;
        return {};
    }

    QVariant headerData(int section, Qt::Orientation o, int role) const override {
        if (o == Qt::Horizontal && role == Qt::DisplayRole)
            return m_headers[section];
        return QAbstractTableModel::headerData(section, o, role);
    }

    void appendData(const PlayerSubtitleItem& it) {
        beginInsertRows({}, m_data.size(), m_data.size());
        m_data.append(it);
        endInsertRows();
    }
    void clearData() {
        beginResetModel();
        m_data.clear();
        endResetModel();
    }

private:
    QStringList m_headers;
    
};